
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
util.PrecacheSound( "Harvest/shockwave.wav" )

function ENT:Initialize()

	self.Entity:SetName("Shockwave Warhead")
	self.Entity:SetModel( "models/items/combine_rifle_ammo01.mdl" )

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(false)
		phys:EnableDrag(false)
		phys:EnableCollisions(false)
	end

	self.PhysObj = self.Entity:GetPhysicsObject()
	self.STime = CurTime()
	self.BTime = self.STime + 3

	self.Entity:SetNetworkedFloat( "FireTime", CurTime() )

	local FlatteningShockwavePain = ents.Create( "point_hurt" )
	FlatteningShockwavePain:SetName("Shockwave")
	FlatteningShockwavePain:SetParent( self.Entity )
	FlatteningShockwavePain:SetOwner( self.Entity:GetOwner() )
	FlatteningShockwavePain:SetKeyValue("DamageRadius", "250" )
	FlatteningShockwavePain:SetKeyValue("Damage", "20" )
	FlatteningShockwavePain:SetKeyValue("DamageDelay", "0.1" )
	FlatteningShockwavePain:SetKeyValue("DamageType", "1024" )
	FlatteningShockwavePain:SetPos( self.Entity:GetPos() )
	FlatteningShockwavePain:Fire("turnon", "", 0);
	FlatteningShockwavePain:Spawn()
	FlatteningShockwavePain:Activate()

	self.ShakeIt = ents.Create( "env_shake" )
	self.ShakeIt:SetName("Shaker")
	self.ShakeIt:SetParent( self.Entity )
	self.ShakeIt:SetKeyValue("amplitude", "100" )
	self.ShakeIt:SetKeyValue("radius", "100" )
	self.ShakeIt:SetKeyValue("duration", "5" )
	self.ShakeIt:SetKeyValue("frequency", "255" )
	self.ShakeIt:SetPos( self.Entity:GetPos() )
	self.ShakeIt:Fire("StartShake", "", 0);
	self.ShakeIt:Spawn()
	self.ShakeIt:Activate()

	self.Entity:Fire("kill", "", 1);
	
end

function ENT:PhysicsUpdate()

end


function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 36

	local ent = ents.Create( "plasburst_head" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Think()
	self.Entity:SetColor(0,0,0,1)

	self.ShakeIt:Fire("StartShake", "", 0)
	
	if (self.Blasted != true) then
		self.Blasted = true
		--self.Entity:EmitSound( "Harvest/shockwave.wav", 500, 100 )
		local x = 0

		for x = 0, 359, 22.5 do
			local Wave = ents.Create( "plasburst_wave" )
			if ( !Wave:IsValid() ) then return end
			Wave:SetPos( self.Entity:GetPos() )
			--local WaveAng = self.Entity:GetAngles()
			--WaveAng.y = WaveAng.y + x
			--if (WaveAng.y > 360) then WaveAng.y = WaveAng.y - 360 end
			--Wave:SetAngles( WaveAng ) 
			Wave:SetAngles( self.Entity:GetAngles() + Angle(0, x, 0) )
			Wave:Spawn()
			Wave:SetOwner( self.Entity )
			Wave:Fire( "kill", "", 0.5)
			Wave.PhysObj:SetVelocity( Wave.Entity:GetForward() * 200 )
		end
		for x = 0, 359, 22.5 do
			local Wave = ents.Create( "plasburst_wave" )
			if ( !Wave:IsValid() ) then return end
			Wave:SetPos( self.Entity:GetPos() )
			--WaveAng = self.Entity:GetAngles()
			--WaveAng.p = WaveAng.p + x
			--if (WaveAng.p > 360) then WaveAng.p = WaveAng.p - 360 end
			--Wave:SetAngles( WaveAng )
			Wave:SetAngles( self.Entity:GetAngles() + Angle(x, 0, 0) )
			Wave:Spawn()
			Wave:SetOwner( self.Entity )
			Wave:Fire( "kill", "", 0.5)
			Wave.PhysObj:SetVelocity( Wave.Entity:GetForward() * 200 )
		end
		for x = 0, 359, 22.5 do
			local Wave = ents.Create( "plasburst_wave" )
			if ( !Wave:IsValid() ) then return end
			Wave:SetPos( self.Entity:GetPos() )
			--WaveAng = self.Entity:GetAngles()
			--WaveAng.p = WaveAng.p + x
			--if (WaveAng.p > 360) then WaveAng.p = WaveAng.p - 360 end
			--WaveAng.y = WaveAng.y + 90
			--if (WaveAng.y > 360) then WaveAng.y = WaveAng.y - 360 end
			--Wave:SetAngles( WaveAng )
			Wave:SetAngles( self.Entity:GetAngles() + Angle(x, 90, 0) )
			Wave:Spawn()
			Wave:SetOwner( self.Entity )
			Wave:Fire( "kill", "", 0.5)
			Wave.PhysObj:SetVelocity( Wave.Entity:GetForward() * 200 )
		end
		--self.Entity:Remove()
	end


end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end